package com.leyou.search.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author fenglin
 * @date 2019/12/22 15:48
 */

@Component
public class ItemListener {

    @Autowired
    private SearchService searchService;

    /**
     *监听商品上架
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(name = MQConstants.Queue.SEARCH_ITEM_UP,durable = "true"),
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_UP_KEY
    ))
    public void ListenerUp(Long spuId){
        //新增商品到索引库
        searchService.addGoods(spuId);
    }

    /**
     * 监听商品下架
     *
     * @param spuId 商品id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.Queue.SEARCH_ITEM_DOWN, durable = "true"),
            exchange = @Exchange(name = MQConstants.Exchange.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY
    ))
    public void listenItemDown(Long spuId) {
        if (spuId != null) {
            // 删除索引库的对应商品
            searchService.deleteGoods(spuId);
        }
    }
}
